package com.nowcoder.controller.interceptor;


import com.nowcoder.entity.User;
import com.nowcoder.service.DataService;
import com.nowcoder.utils.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class DataInterceptor implements HandlerInterceptor {
    @Autowired
    DataService dataService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 记录UV
        String ip = request.getRemoteHost();
        dataService.setUV(ip);
        // 记录DAU
        User user = HostHolder.getUser();
        if(user != null) {
            dataService.setDAU(user.getId());
        }
        return true;
    }
}
